home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Development / 3DMF parser / 1.0 version / MF3DPC / MFEXAMPL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-07  |  5.7 KB  |  181 lines  |  [TEXT/dosa]

  1. /*==============================================================================
  2.  *
  3.  *    File:        MPWRefExample.c
  4.  *
  5.  *    Function:    QuickDraw 3D Metafile Read/Write Sample Code
  6.  *                This code shows how to resolve internal and external references
  7.  *                which are read from a QuickDraw 3D metafile.
  8.  *
  9.  *    Version:    Metafile:    Version 1.0 3DMF files
  10.  *                Package:    Release #2 of this code
  11.  *
  12.  *    Author(s):    Rick Wong (RWW), Duet Development Corp.
  13.  *                John Kelly (JRK), Duet Development Corp.
  14.  *
  15.  *    Copyright:    (c) 1995 by Apple Computer, Inc., all rights reserved.
  16.  *
  17.  *    Change History (most recent first):
  18.  *        F3M_RWW    Added this header.
  19.  *==============================================================================
  20.  */
  21.  
  22. #include <stdio.h>                /* printf */
  23.  
  24. #ifdef __MWERKS__
  25. #include <console.h>            /* ccommand */
  26. #endif
  27.  
  28. #include "MF3D.H"                /* MF3D API            */
  29. #include "MFERRORS.H"            /* MF3D Error codes    */
  30.  
  31. int
  32. main(int argc, char *argv[])
  33. {
  34.     MF3D_FilePtr        metafilePtr;        /* MF3D internal file pointer    */
  35.     MF3DVoidObjPtr        object;                /* object returned by Read        */
  36.     int                    objCount;            /* count the objects as we read    */
  37.     MF3DBoolean            nextObjIsRoot;        /* next obj read will be root    */
  38.     MF3DErr                result;                /* our result code                */
  39.     MF3DErr                status;                /* temporaty result code        */
  40.  
  41. #ifdef __MWERKS__
  42.     argc = ccommand(&argv);
  43. #endif
  44.  
  45.     if (argc != 2)
  46.     {    printf("# Usage: %s fileName\n", argv[0]);
  47.         return -1;
  48.     }
  49.  
  50.     result = kMF3DNoErr;
  51.  
  52.     if (result == kMF3DNoErr)
  53.     {    /* Open the metafile */
  54.         result = MF3DOpenInputStdCFile(argv[1], &metafilePtr);
  55.         if (result != kMF3DNoErr)
  56.             printf("# Open Input failed. Error %ld.\n", (long)result);
  57.     }
  58.  
  59.     objCount = 0;
  60.     nextObjIsRoot = kMF3DBooleanFalse;
  61.  
  62.     while (result == kMF3DNoErr)
  63.     {    /* Read an object from the metafile */
  64.         result = MF3DReadAnObject(metafilePtr, &object);
  65.  
  66.         ++objCount;
  67.  
  68.         if (result != kMF3DNoErr)
  69.         {    if (result != kMF3DNoMoreObjects)
  70.             {    printf("# Read failed for object #%d. Error %ld.\n", objCount,
  71.                         (long)result);
  72.             }
  73.         }
  74.         else if (object->objectType == kMF3DObjUnknownType)
  75.         {    MF3DObjType objectType = ((MF3DUnknownObjPtr)object)->realObjectType;
  76.             char *objectName = ((MF3DUnknownObjPtr)object)->realObjectName;
  77.             if (objectType == kMF3DObjUnknownType)
  78.                 objectType = 0x20202020;    /* Four spaces */
  79.             if (objectName == NULL)
  80.                 objectName = "";            /* Empty string */
  81.             printf("# Read succeeded for unknown object #%d (%.4s, \"%s\").\n",
  82.                     objCount, &objectType, objectName);
  83.         }
  84.         else
  85.         {    printf("# Read succeeded for object #%d (%.4s).\n", objCount,
  86.                     &object->objectType);
  87.         }
  88.  
  89.         if (result == kMF3DNoErr && object->objectType == kMF3DObjReference)
  90.         {    /* If we have just read a Reference object, resolve it.
  91.              * There are two types of references: local and external.
  92.              * An external reference will be the root object of a container
  93.              * and will have a subobject specifying the path
  94.              * to an external file.
  95.              */
  96.             MF3DVoidObjPtr    extStorageObj;
  97.  
  98.             extStorageObj = NULL;
  99.             if (nextObjIsRoot == kMF3DBooleanTrue)
  100.             {    /* We are in a container which must mean we have an
  101.                  * external reference.
  102.                  */
  103.                 result = MF3DReadAnObject(metafilePtr, &extStorageObj);
  104.  
  105.                 ++objCount;
  106.                 if (result != kMF3DNoErr)
  107.                 {    printf("# Read failed for object #%d. Error %ld.\n",
  108.                             objCount, (long)result);
  109.                 }
  110.                 else
  111.                 {    printf("# Read succeeded for object #%d (%.4s).\n",
  112.                             objCount, &extStorageObj->objectType);
  113.                 }
  114.  
  115.                 /* Because this sample code is written on a Macintosh,
  116.                  * we expect a Macintosh-style path here.
  117.                  */
  118.                 if (result == kMF3DNoErr &&
  119.                         extStorageObj->objectType != kMF3DObjMacintoshPath)
  120.                 {    printf("# Resolve external reference failed because "
  121.                             "external storage is not a Macintosh path.\n");
  122.                     result = -2;
  123.                 }
  124.             }
  125.  
  126.             /* Resolve the reference. This causes metafilePtr to point
  127.              * to the referenced object, so that the next call to
  128.              * MF3DReadAnObject will read the object which is being
  129.              * referenced. MF3DReadAnObject will then restore the correct
  130.              * file position after the referenced object has been read.
  131.              *
  132.              * If the reference is local, then extStorageObj is NULL.
  133.              * If the reference is external, then the file specified by
  134.              * extStorageObj will be opened automatically (and closed
  135.              * automatically after the reference has been resolved).
  136.              */
  137.             if (result == kMF3DNoErr)
  138.             {    result = MF3DResolveReference(metafilePtr,
  139.                         (MF3DReferenceObjPtr) object,
  140.                         (MF3DStorageObjPtr) extStorageObj);
  141.             }
  142.  
  143.             /* Dispose the external storage object, if there is one.
  144.              * The Reference object will be disposed later.
  145.              */
  146.             MF3DDisposeObject(extStorageObj);
  147.         }
  148.  
  149.         /* If the object we just read is a BeginContainer object, the next
  150.          * object read will be the root object of the container.
  151.          */
  152.         if (result == kMF3DNoErr && object->objectType == kMF3DObjContainer)
  153.             nextObjIsRoot = kMF3DBooleanTrue;
  154.         else
  155.             nextObjIsRoot = kMF3DBooleanFalse;
  156.  
  157.         /* Dispose the object (a real program would copy data first) */
  158.         if (result == kMF3DNoErr)
  159.         {    result = MF3DDisposeObject(object);
  160.             if (result != kMF3DNoErr)
  161.             {    printf("# Dispose failed for object #%d (%.4s). Error %ld.\n",
  162.                         objCount, &object->objectType, (long)result);
  163.             }
  164.         }
  165.     }
  166.  
  167.     /* No error occurred if we ran out of objects while reading */
  168.     if (result == kMF3DNoMoreObjects)
  169.         result = kMF3DNoErr;
  170.  
  171.     /* Close does nothing if metafilePtr is NULL */
  172.     status = MF3DClose(metafilePtr);
  173.     if (status != kMF3DNoErr)
  174.     {    printf("# Close input failed. Error %ld.\n", (long)status);
  175.         if (result == kMF3DNoErr)
  176.             result = status;
  177.     }
  178.  
  179.     return result;
  180. }
  181.